package cn.ii8080.i8.web;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.Map;

import static cn.ii8080.i8.web.HttpStatus.*;

/**
 * 返回结果
 *
 * @param <T>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultVo<T> {
    public final static int SUCCESS_CODE = 0;
    public final static String SUCCESS_MSG = "success!";
    public final static int ERROR_CODE = 9999;
    public final static String ERROR_MSG = "error!";
    /**
     * HttpStatus
     * 状态码
     */
    @JsonIgnore
    private int status = SC_OK;
    /**
     * 返回码
     */
    private Integer code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 错误消息
     */
    private Map<String, Object> errs;
    /**
     * 数据
     */
    private T data;

    private Long timestamp;

    public ResultVo() {

    }

    public static <T> ResultVo<T> success() {
        ResultVo<T> success = new ResultVo<>();
        success.setStatus(SC_OK);
        success.setCode(SUCCESS_CODE);
        success.setMsg(SUCCESS_MSG);
        return success;
    }

    public static <T> ResultVo<T> success(T data) {
        ResultVo<T> success = success();
        success.setData(data);
        return success;
    }

    public static <T> ResultVo<T> success(String msg) {
        ResultVo<T> success = success();
        success.setMsg(msg);
        return success;
    }

    public static <T> ResultVo<T> error() {
        ResultVo<T> error = new ResultVo<>();
        error.setStatus(SC_INTERNAL_SERVER_ERROR);
        error.setCode(ERROR_CODE);
        error.setMsg(ERROR_MSG);
        return error;
    }

    public static <T> ResultVo<T> error(int code) {
        ResultVo<T> error = error();
        error.setCode(code);
        return error;
    }

    public static <T> ResultVo<T> error(String msg) {
        ResultVo<T> error = error();
        error.setMsg(msg);
        return error;
    }

    public int getStatus() {
        return status;
    }

    public ResultVo<T> setStatus(int status) {
        this.status = status;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public ResultVo<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ResultVo<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Map<String, Object> getErrs() {
        return errs;
    }

    public ResultVo<T> setErrs(Map<String, Object> errs) {
        this.errs = errs;
        return this;
    }

    public T getData() {
        return data;
    }

    public ResultVo<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }
}
